package com.code.app.floatview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class ProgressView extends View {

    private Paint circlePaint;

    private Paint textPaint;

    private Paint progressPaint;

    private Canvas bitmapCanvas;

    private Bitmap bitmap;

    private int width;

    private int height;

    private Path path;

    private int progress = 30;

    private int max = 100;

    public ProgressView(Context context) {
        super(context);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setColor(Color.GREEN);
        circlePaint.setAntiAlias(true);

        progressPaint = new Paint();
        progressPaint.setColor(Color.YELLOW);
        progressPaint.setAntiAlias(true);
        progressPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(sp2px(25));

        width = height = (int) dp2px(150);
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);

        path = new Path();


    }

    private float sp2px(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    private float dp2px(int dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bitmapCanvas.drawCircle(width / 2, height / 2, height / 2, circlePaint);
        path.reset();
        float y = ((float)(1 - ((float) progress / max))) * height;
        path.moveTo(width, y);
        path.moveTo(width, height);
        path.moveTo(0, height);
        path.moveTo(0, y);
        for (int i = 0; i < 3; i++) {
            path.rQuadTo(10, -10, 20, 0);
            path.rQuadTo(10, 10, 20, 0);
        }
        path.close();
        bitmapCanvas.drawPath(path, progressPaint);

        int currentProgress = (int) (((float) progress / max) * 100);
        String text = currentProgress + "%";
        int dx = (int) (width / 2 - textPaint.measureText(text) / 2);

        Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        int baseLine = height / 2 + dy;
        bitmapCanvas.drawText(text, dx, baseLine, textPaint);
        canvas.drawBitmap(bitmap, 0, 0, null);
    }
}
